#include <iostream>
#include <functional>

using std::cout;
using std::endl;
using std::bind;
using std::function;

int add(int x, int y)
{
    cout << "int add(int, int) " << endl;
    return x + y;
}

int multiply(int x, int y, int z)
{
    cout << "int multiply(int, int, int)" << endl;
    return x * y * z;
}

class Example
{
public:
    int add(int x, int y)
    {
        cout << "int Example::add(int, int)" << endl;
        return x + y;
    }

    int data = 250;//C++11
};

void test()
{
    //add函数是返回类型是int，参数是int,int
    //函数的类型（标签，形态）int(int, int)
    //f的形态:int()
    /* auto f = bind(add, 1, 2); */
    function<int()> f = bind(add, 1, 2);
    cout << "f() = " << f() << endl;

    //int(int, int, int)
    //f2的形态:int()
    /* auto f2 = bind(&multiply, 3, 4, 5); */
    function<int()> f2 = bind(&multiply, 3, 4, 5);
    /* f = bind(&multiply, 3, 4, 5); */
    cout << "f2() = " << f2() << endl;

    Example ex;
    //int(Example *, int, int)
    //f3的形态:int()
    /* auto f3 = bind(&Example::add, &ex, 10, 30); */
    function<int()> f3 = bind(&Example::add, &ex, 10, 30);
    cout << "f3() = " << f3() << endl;

    using namespace std::placeholders;
    //占位符
    //f4的形态:int(int)
    /* auto f4 = bind(add, 10, _1); */
    function<int(int)> f4 = bind(add, 10, _1);
    /* cout << "f4(20, 40) = " << f4(20, 40) << endl; */
    cout << "f4(40) = " << f4(40) << endl;

    function<int()> f5 = bind(&Example::data, &ex);
    cout << "f5() = " << f5() << endl;
}

void test2()
{
    Example ex;
    function<int()> f = bind(&Example::add, &ex, 10, 30);
    cout << "f() = " << f() << endl;

    cout << endl;
    function<int()> f2 = bind(&Example::add, ex, 10, 30);
    cout << "f2() = " << f2() << endl;
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

